mui.init();

//图片预览
mui.previewImage();

//获取本地信息
//var my_phone = localStorage.getItem("my_phone");//手机号
var user_id = localStorage.getItem("user_id");
var is_this;

//动态详情
function dynamic_details(dynamicId) { 
	interfRequest.find_dynamic_by_id.data = {
		dynamic_id: dynamicId,
		user_id: user_id
	}
	socialRequest(interfRequest.find_dynamic_by_id); //异常处理 
}

//评论列表
function dynamic_review(dynamicId) { 
	interfRequest.dynamic_review.data = {
		dynamic_id: dynamicId,
		number: 0,
		pagesize: 10
	}
	socialRequest(interfRequest.dynamic_review); //异常处理 

}

//关注用户
mui('body').on('tap', '.follow', function() {
	var publisher = $(".details_image").attr("id"); //用户id  
	interfRequest.concern.data = {
		user_id: user_id,
		passive_user_id: publisher //true	普通参数	int		我关注的人id
	}
	socialRequest(interfRequest.concern); //异常处理  

})

//点赞动态
mui('body').on('tap', '.publish_left', function() {
	var dynamic = $("#dynamic").val(); //动态id 
	is_this = $(this);
	interfRequest.dynamic_label.data = {
		user_id: user_id,
		dynamic_id: dynamic, //true	普通参数	int		动态id
	}
	socialRequest(interfRequest.dynamic_label); //异常处理   
});

/*点赞*/
function dynamic_labelHandle(data) {
	//console.log(JSON.stringify(data));
	if(data.error_code == 10000) {
		is_this.find(".iconfont").addClass("all_icon_color");
		var dz_num = is_this.find(".iconfont").siblings().text(); 
		is_this.find(".iconfont").siblings().text(parseInt(dz_num) + 1);
	} else {
		$("#zan").val(data.error_code);
		//mui.toast("该条动态已赞");   
	}
}

//点击评论
$(document).on('tap', '#input_btn', function() {
	$('.comment_popup').css('display', 'block'); //显示弹窗
	jianpan(); //打开键盘
	var comment_id = $(this).parent().parent().attr("sta"); //评论id  
	var user = $(this).parent().parent().attr("id"); //评论id  
	$(".comment_id").val(comment_id);
	$(".user").val(user);
	$(".commenttype").val(1); //评论类型 
});

//回复
$(document).on('tap', '.details_comment_text', function() {
	$('.comment_popup').css('display', 'block'); //显示弹窗 
	var comment_id = $(this).parent().parent().attr("sta"); //评论id  
	var user = $(this).parent().parent().attr("id"); //评论id   
	$(".comment_id").val(comment_id);
	$(".user").val(user);
	$(".commenttype").val(2); //评论类型 
});

//打开键盘
function jianpan() {
	//打开搜索键盘
	var nativeWebview, imm, InputMethodManager;
	var initNativeObjects = function() {
		if(mui.os.android) {
			var main = plus.android.runtimeMainActivity();
			var Context = plus.android.importClass("android.content.Context");
			InputMethodManager = plus.android.importClass("android.view.inputmethod.InputMethodManager");
			imm = main.getSystemService(Context.INPUT_METHOD_SERVICE);
		} else {
			nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
		}
	};
	var showSoftInput = function() {
		var nativeWebview = plus.webview.currentWebview().nativeInstanceObject();
		if(mui.os.android) {
			//强制当前webview获得焦点
			plus.android.importClass(nativeWebview);
			nativeWebview.requestFocus();
			imm.toggleSoftInput(0, InputMethodManager.SHOW_FORCED);
		} else {
			nativeWebview.plusCallMethod({
				"setKeyboardDisplayRequiresUserAction": false
			});
		}
		setTimeout(function() {
			//此处可写具体逻辑设置获取焦点的input
			var inputElem = document.querySelector('#text_comment');
			inputElem.focus();
		}, 200);
	};
}

//发送评论(动态)
$(document).on('tap', '#send', function() {
	var text_comment = $("#text_comment").val(); //评论内容
	var dynamic = $("#dynamic").val(); //动态id 
	//var dynamic_type = $("#dynamic_type").val(); //动态转态
	var nickname = $(".details_image .details_image_name").text(); //昵称
	var comment_id = $(".comment_id").val();
	var user = $(".user").val();
	var commenttype = $(".commenttype").val(); //评论类型  
	if(text_comment == "") {
		mui.toast("您还没有输入评论内容");
	} else {
		interfRequest.comment_dynamics.data = {
			dynamicId: dynamic, //true	普通参数	int		动态id
			content: text_comment, //true	普通参数	string		评论内容
			userId: user, //true	普通参数	string		评论用户id
			username: nickname, //true	普通参数	string		用户名
			parentId: comment_id, //true	普通参数	int		评论id
			comment_type: commenttype //true	普通参数	int		评论标记：1、针对动态；2、回复（回复时才有上一条动态的关联信息）
		}
		socialRequest(interfRequest.comment_dynamics); //异常处理  
	}
	$('.comment_popup').css('display', 'none');

});

//举报
mui('body').on('tap', '#report', function() {
	var dynamicid = $("#dynamic").val(); //动态id
	var passiveid = $(".details_image").attr("id"); //用户id//动态用户id
	var username = $(".details_image_name").text(); //动态用户昵称  
	mui.openWindow({
		url: '../index/Report.html',
		id: 'Report',
		extras: {
			dynamic_id: dynamicid,
			passive_id: passiveid,
			passive_username: username
		},
		createNew: true
	});
});

//点击评论区之外的关闭弹窗
$(document).bind("click", function(e) {
	var target = $(e.target);
	if(target.closest(".comment_popup,.details_comment_text").length == 0) {
		$(".comment_popup").css('display', 'none');
	}
});

//查看更多评论
mui('body').on('tap', '.more_comment', function() {
	var id = $("#dynamic").val(); //动态id 
	mui.openWindow({
		url: '../video/video_reply_content.html',
		extras: {
			publisher: id,
		},
		createNew: true
	});

})

//页面进入执行方法
window.onload = function() {
	mui.plusReady(function() {
		var self = plus.webview.currentWebview();
		var dynamicId = self.dynamic_id;
		$("#dynamic").val(dynamicId); //动态id  
		dynamic_details(dynamicId); //动态详情
		dynamic_review(dynamicId); //评论列表
	});
}